package com.besttone.util;

/**
 * Created by zhengzhenzhen on 2017/7/19.
 */

        import java.io.IOException;
        import java.io.UnsupportedEncodingException;
        import java.util.ArrayList;
        import java.util.List;
        import java.util.Map;
        import java.util.Set;
        import org.apache.http.HttpEntity;
        import org.apache.http.HttpResponse;
        import org.apache.http.NameValuePair;
        import org.apache.http.client.ClientProtocolException;
        import org.apache.http.client.HttpClient;
        import org.apache.http.client.entity.UrlEncodedFormEntity;
        import org.apache.http.client.methods.HttpPost;
        import org.apache.http.impl.client.DefaultHttpClient;
        import org.apache.http.message.BasicNameValuePair;
        import org.apache.http.util.EntityUtils;



/**
 *
 */
public class HttpClientUtil {

    enum EnumHttpClientResultCode{
        SUCCESS,//成功
        FAILED,//失败
        TIMEOUT//超时
    }
    public static class HttpClientResult{
        private EnumHttpClientResultCode code;
        private String value;
        private String message;


        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }

        public boolean isSuccess(){
            return EnumHttpClientResultCode.SUCCESS==this.code;
        }

        public boolean isTimeout(){
            return EnumHttpClientResultCode.TIMEOUT==this.code;
        }

        public boolean isFailed(){
            return EnumHttpClientResultCode.FAILED==this.code;
        }
        public EnumHttpClientResultCode getCode() {
            return code;
        }
        public void setCode(EnumHttpClientResultCode code) {
            this.code = code;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }



    }
    /**
     * POST的请求
     * @param url
     * @param params
     * @return
     * @author zzy
     */
    public static HttpClientResult httpClientOfPost(String url,Map params){
        HttpClientResult result=new HttpClientResult();

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        if(params!=null){
            Set<String> keySet=params.keySet();
            for(String key:keySet){
                formparams.add(new BasicNameValuePair(key, ObjectUtil.defaultIfNull(params.get(key), "").toString()));
            }
        }
        try {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(entity);
            HttpClient hclient=new DefaultHttpClient();
            HttpResponse response=hclient.execute(httppost);
            HttpEntity httpEntity=response.getEntity();
            String rspVal=EntityUtils.toString(httpEntity);
            result.setValue(rspVal);
            result.setCode(EnumHttpClientResultCode.SUCCESS);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            result.setCode(EnumHttpClientResultCode.FAILED);
            result.setMessage(e.toString());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            result.setCode(EnumHttpClientResultCode.FAILED);
            result.setMessage(e.toString());
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(EnumHttpClientResultCode.TIMEOUT);
            result.setMessage(e.toString());
        }
        return result;
    }


}
